<?php
/**
* @package Frontend-User-Access (com_frontenduseraccess)
* @version 3.0.8
* @copyright Copyright (C) 2008 Carsten Engel. All rights reserved.
* @license GPL versions free/trial/pro
* @author http://www.pages-and-items.com
* @joomla Joomla is Free Software
*/

// no direct access
defined('_JEXEC') or die('Restricted access');


class frontenduseraccessMenuAccessChecker{	

	var $version_type = 'pro';
	
	function get_config(){	
	
		static $raw_fua_config;
		
		if(!$raw_fua_config){
			$database = JFactory::getDBO();			
			
			$database->setQuery("SELECT config "
			."FROM #__fua_config "
			."WHERE id='fua' "
			."LIMIT 1"
			);		
			$raw_fua_config = $database->loadResult();		
		}
		
		return $raw_fua_config;			
	}
	
	function filter_menu_items($rows, $menutype){		
	
		$user = JFactory::getUser();
		$user_type = $user->get('usertype');
		
		$trial_access = 1;
		if($this->version_type=='trial'){
			$trial_access = 0;
			require_once(dirname(__FILE__).'/../../administrator/components/com_frontenduseraccess/controller.php');
			$fua_controller = new frontenduseraccessController();
			$trial_access = $fua_controller->fua_check_trial_version();
		}		
		
		if($user_type=='Super Administrator' || !$trial_access){
			//dont restrict anything
			return $rows;
		}else{			
			
			$raw_fua_config = $this->get_config();
			
			//if menuaccess is activated
			if(strpos($raw_fua_config ,'use_menuaccess=true')){
						
				$fua_user_group = $this->get_fua_usergroup();
				//echo $fua_user_group;				
									
				$database = JFactory::getDBO();	
				//get groups menu-access
				$database->setQuery("SELECT menuid_groupid "
				."FROM #__fua_menuaccess "		
				);
				$menu_access_array = $database->loadResultArray();
				
				$menu_id_string = '8.8';//somehow a number needs to be in there when no restrictions are set or query goes nuts.						
				foreach($menu_access_array as $menu_access){
					$temp = explode('_', $menu_access);	
					if($temp[1]==$fua_user_group){
						$menu_id_string .= ',';								
						$menu_id_string .= $temp[0];
					}
				}
				
				if(!$menutype){
					$menutype = 'mainmenu';
				}
				
				
				if(strpos($raw_fua_config ,'menu_reverse_access=true')){
					$where_in = "(id NOT in ($menu_id_string))";
				}else{
					$where_in = "(id in ($menu_id_string))";
				}				
				
				$where_access = '';
				if(!$user_type){
					$where_access = "AND access='0' ";
				}elseif($user_type=='Registered'){
					$where_access = "AND (access='0' OR access='1') ";
				}elseif($user_type=='Author' || $user_type=='Editor' || $user_type=='Publisher' || $user_type=='Manager' || $user_type=='Administrator'){				
					$where_access = "AND (access='0' OR access='1' OR access='2') ";
				}
				
				$database->setQuery("SELECT * "
				."FROM #__menu "	
				."WHERE $where_in AND menutype='$menutype' AND published='1' $where_access "
				."ORDER BY parent, ordering "
				);
				$new_rows = $database->loadObjectList();
				return $new_rows;					
			}else{
				return $rows;
			}
		}
	}
	
	function get_fua_usergroup(){
		
		static $fua_usergroup_id_unique;
	
		if(!$fua_usergroup_id_unique){
			$user = JFactory::getUser();
			$user_id = $user->get('id');					
				
			//get FUA usergroup of user
			if(!$user_id){	
				//not logged in
				$group_id = 10;
			}else{	
				$database = JFactory::getDBO();					
				$database->setQuery("SELECT group_id "
				."FROM #__fua_userindex  "
				."WHERE user_id='$user_id' "
				."LIMIT 1 "
				);
				$users = $database->loadObjectList();
				$group_id = false;
				foreach($users as $userrow){	
					$group_id = $userrow->group_id;	
				}
				if(!$group_id){
					//user is not assigned to any group
					$group_id = 9;
				}
			}
			$fua_usergroup_id_unique = $group_id;
		}
		return $fua_usergroup_id_unique;		
	}

	
	
	

}
?>